package com.persondemo.videoappliction.ui.search;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import com.persondemo.videoappliction.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/16.
 * 不用x5内核
 */

public class SeachWebActivity extends Activity {
    String url = "";
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.search_web)
    WebView webView;
    @BindView(R.id.pb_progress)
    ProgressBar pbProgress;
    private WebSettings settings;
    private InterstitialAd interstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.module_activity_seachweb);
        ButterKnife.bind(this);
        init();
    }
    private void init() {
//        MobileAds.initialize(this, "ca-app-pub-5628217081024947~2270121142");
//        // Create the InterstitialAd and set the adUnitId.
//        interstitialAd = new InterstitialAd(this);
//        // Defined in res/values/strings.xml
//        interstitialAd.setAdUnitId("ca-app-pub-5628217081024947/2943202273");
//        showInterstitial();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent intent = getIntent();
        if (intent != null) {
            url = intent.getStringExtra("title");
            settings = webView.getSettings();
            //支持javascript
            webView.getSettings().setJavaScriptEnabled(true);
// 设置可以支持缩放
            webView.getSettings().setSupportZoom(true);
// 设置出现缩放工具
            webView.getSettings().setBuiltInZoomControls(false);
//扩大比例的缩放
            webView.getSettings().setUseWideViewPort(true);
//自适应屏幕
            webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
            webView.getSettings().setLoadWithOverviewMode(true);
            settings.setDisplayZoomControls(true);
            settings.setDomStorageEnabled(true);
            webView.setWebViewClient(new WebViewClient());
            webView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    if (newProgress == 100) {
                        // 网页加载完成
                        pbProgress.setVisibility(View.GONE);
                    } else {
                        // 加载中
                        pbProgress.setProgress(newProgress);
                        pbProgress.setVisibility(View.VISIBLE);
                    }
                    super.onProgressChanged(view, newProgress);
                }
            });
            webView.loadUrl("http://vip.axiaodao.com/api/api/vipheader&href=" + url);
            Log.i("guanggao", "http://vip.axiaodao.com/api/api/vipheader&href=" + url);
        }
    }

    private void showInterstitial() {
        // Show the ad if it's ready. Otherwise toast and restart the game.
        if (interstitialAd != null && interstitialAd.isLoaded()) {
            interstitialAd.show();
        } else {
            Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
            startGame();
        }
    }

    private void startGame() {
        // Request a new ad if one isn't already loaded, hide the button, and kick off the timer.
        if (!interstitialAd.isLoading() && !interstitialAd.isLoaded()) {
            AdRequest adRequest = new AdRequest.Builder().build();
            interstitialAd.loadAd(adRequest);
        }
//        resumeGame(GAME_LENGTH_MILLISECONDS);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webView != null) {
            webView.setVisibility(View.GONE);
            webView.destroy();
            webView = null;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //如果按下的是回退键且历史记录里确实还有页面
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

//    @Override
//    public boolean isSupportSwipeBack() {
//        return false;
//    }
}
